package com.mlh.schedulejob;


import com.mlh.entity.Article;
import com.mlh.service.ArticleService;
import com.mlh.util.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 缪林辉
 * @date 2024/3/17 14:28
 * @DESCRIPTION
 */
@Component
public class ViewCountSchedule {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ArticleService articleService;

    //@Scheduled(cron = "* 0/10 * * * ?")//表示该任务每10中执行一次
    public void viewCountUpdate(){
        //从redis取出map<id,viewCount>
        Map<String,Integer> viewCountMap = redisCache.getCacheMap("article:viewCount");
        List<Article> articles = viewCountMap.entrySet().stream().map(entry -> {
            return new Article(Long.valueOf(entry.getKey()), entry.getValue().longValue());
        }).collect(Collectors.toList());
        //批量更新
        // mapper 和 service 方法  有点分不清楚  ？？？？？？？？？？？？？
        articleService.updateBatchById(articles);
    }
}
